﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of DebtChasedWebsiteMenuCtrl class
	/// </summary>
	public partial class DebtChasedWebsiteMenuCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Enumerations

		public enum eMenuItem
		{

			NotSet = 0,

			SearchPopup,

			ChaseScreen,
			ChaseScreen_Contacts,
			ChaseScreen_DebtDetails,
            ChaseScreen_Communications,
            ChaseScreen_Tools,
            ChaseScreen_Comments,
            ChaseScreen_Agent,

			//ChaseScreen_Expected,
			//ChaseScreen_Invoicing,
            //ChaseScreen_Summary -> Tools
			//ChaseScreen_Documents,

			//MyAccount,
			//MyAccount_UserManagement,
            UserManagement,
			Dashboard,

			Admin,
            Admin_Agents,
            Admin_Communications,
            Admin_Financial,
            Admin_ImportWizard,
            Admin_Invoicing,
			Admin_BulkActions,
            Admin_Configurations,

			Workflow,
			ClientControls,
            ClientControl_Settings,
            ClientControl_Rates,
            ClientControl_Reports,
            ClientControl_Cases,

            //InvoiceControls,

			Reports,
			//Reports_Top20Accounts,

			SignOut,

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private bool _bAllowChaseDrop = false;
		public bool AllowChaseDrop
		{
			get { return _bAllowChaseDrop; }
			set 
            { 
				_bAllowChaseDrop = value;
                menuChaseScreenShowToggle.Visibility = _bAllowChaseDrop ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			}
		}

		private eMenuItem _highlightMenuItem = eMenuItem.NotSet;
		public eMenuItem HighlightMenuItem
		{
			get { return _highlightMenuItem; }
			set 
            {
				_setHighlight(_highlightMenuItem,false);
				_highlightMenuItem = value;
				_setHighlight(_highlightMenuItem,true);
			}
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public DebtChasedWebsiteMenuCtrl()
		{

			// Load xaml resources
			InitializeComponent();

			// Set up animation ranges
			dbanExpandChaseGrid.To = gridChase.MaxHeight;
			dbanContractChaseGrid.To = gridChase.MinHeight;
			dbanExpandChaseGrid.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 40 * gridChase.RowDefinitions.Count));
			dbanContractChaseGrid.Duration = dbanExpandChaseGrid.Duration;

			dbanExpandAdminGrid.To = gridAdmin.MaxHeight;
			dbanContractAdminGrid.To = gridAdmin.MinHeight;
			dbanExpandAdminGrid.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 40 * gridAdmin.RowDefinitions.Count));
			dbanContractAdminGrid.Duration = dbanExpandAdminGrid.Duration;

			dbanExpandClientControlGrid.To = gridClientControl.MaxHeight;
			dbanContractClientControlGrid.To = gridClientControl.MinHeight;
			dbanExpandClientControlGrid.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 40 * gridClientControl.RowDefinitions.Count));
			dbanContractClientControlGrid.Duration = dbanExpandClientControlGrid.Duration;

			// Reset grid heights
			gridChase.Height = gridChase.MinHeight;
			gridAdmin.Height = gridAdmin.MinHeight;
			gridClientControl.Height = gridClientControl.MinHeight;

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

        private void menuChaseScreenShowToggle_Click(object sender, EventArgs e)
        {
            menuChaseScreenShowToggle.IsUpsideDown = !menuChaseScreenShowToggle.IsUpsideDown;
            if (menuChaseScreenShowToggle.IsUpsideDown)
                AnimRaiseChaseMenu.Begin();
            else
                AnimDropChaseMenu.Begin();
        }

        private void gridChase_MouseEnter(object sender, MouseEventArgs e)
		{
			if (_bAllowChaseDrop)
				AnimDropChaseMenu.Begin();
		}

		private void gridChase_MouseLeave(object sender, MouseEventArgs e)
		{
            if (menuChaseScreenShowToggle.IsUpsideDown)
				AnimRaiseChaseMenu.Begin();
		}

        private void menuAdminShowToggle_Click(object sender, EventArgs e)
        {
            menuAdminShowToggle.IsUpsideDown = !menuAdminShowToggle.IsUpsideDown;
            if (menuAdminShowToggle.IsUpsideDown)
                AnimRaiseAdminMenu.Begin();
            else
                AnimDropAdminMenu.Begin();
        }

		private void gridAdmin_MouseEnter(object sender, MouseEventArgs e)
		{
			AnimDropAdminMenu.Begin();
		}

		private void gridAdmin_MouseLeave(object sender, MouseEventArgs e)
		{
            if (menuAdminShowToggle.IsUpsideDown)
				AnimRaiseAdminMenu.Begin();
		}

        private void menuClientControlShowToggle_Click(object sender, EventArgs e)
        {
            menuClientControlShowToggle.IsUpsideDown = !menuClientControlShowToggle.IsUpsideDown;
            if (menuClientControlShowToggle.IsUpsideDown)
                AnimRaiseClientControlMenu.Begin();
            else
                AnimDropClientControlMenu.Begin();
        }

		private void gridClientControl_MouseEnter(object sender, MouseEventArgs e)
		{
			AnimDropClientControlMenu.Begin();
		}

		private void gridClientControl_MouseLeave(object sender, MouseEventArgs e)
		{
            if (menuClientControlShowToggle.IsUpsideDown)
				AnimRaiseClientControlMenu.Begin();
		}


		/////////////////////////////////////////////////////////////
		// Operations

		public void MenuItemClicked(eMenuItem menuItem)
		{

			// Compare menu items
			switch (menuItem)
			{
				case eMenuItem.SearchPopup:
					MessageBoxes.PopupDevMessage();
					break;

				case eMenuItem.ChaseScreen:
					_requestUrl("DebtChaser.aspx");
                    break;

                case eMenuItem.ChaseScreen_Contacts:
				case eMenuItem.ChaseScreen_DebtDetails:
				case eMenuItem.ChaseScreen_Communications:
				case eMenuItem.ChaseScreen_Tools:
				case eMenuItem.ChaseScreen_Comments:
				case eMenuItem.ChaseScreen_Agent:
					LibrarySession.Singleton.IMainPage.WebsiteMenuClicked(menuItem);
					break;


				case eMenuItem.UserManagement:
					_requestUrl("UserManagement.aspx");
					break;

				case eMenuItem.Dashboard:
					_requestUrl("Dashboard.aspx");
					break;

                case eMenuItem.Admin:
#if DEBUG
					Debug.Assert(false);
#endif // #if DEBUG
					break;
				case eMenuItem.Admin_Agents:
					_requestUrl("AgentDetails.aspx");
					break;
				case eMenuItem.Admin_Communications:
					_requestUrl("CommsQueue.aspx");
					break;
				case eMenuItem.Admin_Financial:
					_requestUrl("Financial.aspx");
					break;
				case eMenuItem.Admin_ImportWizard:
					_requestUrl("ImportWizard.aspx");
					break;
				case eMenuItem.Admin_Invoicing:
					//LibrarySession.Singleton.IMainPage.WebsiteMenuClicked(menuItem);
                    _requestUrl("InvoicingScreen.aspx");
					break;
				case eMenuItem.Admin_BulkActions:
					_requestUrl("BulkActions.aspx");
					break;
				case eMenuItem.Admin_Configurations:
					_requestUrl("Config.aspx");
					break;

                //case eMenuItem.InvoiceControls:
                //    _requestUrl("InvoicingScreen.aspx");
                //        break;

				case eMenuItem.Workflow:
					_requestUrl("Workflows.aspx");
					break;

				case eMenuItem.ClientControls:
					_requestUrl("ClientDetails.aspx");
					break;

                case eMenuItem.ClientControl_Settings:
                case eMenuItem.ClientControl_Rates:
                case eMenuItem.ClientControl_Reports:
                case eMenuItem.ClientControl_Cases:
 					MessageBoxes.PopupDevMessage();
                   break;


				case eMenuItem.Reports:
#if DEBUG
					Debug.Assert(false);
#endif // #if DEBUG
					break;

				case eMenuItem.SignOut:
					if (MessageBoxes.PopupOption("Do you want to close your Debt Chased session?"))
						_requestUrl("../Default.aspx?act=signout");
					break;

				default:
#if DEBUG
					Debug.Assert(false);
#endif // #if DEBUG
					break;

			}
		}

		private void _requestUrl(string strUrl)
		{

			try
			{

				// Open the website
				Uri uri = new Uri(strUrl, UriKind.Relative);
				HtmlPage.Window.Navigate(uri, "_self", "");

			}
			catch (Exception x)
			{

				// Message for user
				MessageBoxes.PopupMessage(x.Message);

			}

		}

		private WebsiteMenuItemCtrl _getMenuItem(eMenuItem menuItemId)
		{

			// Declare return variable
			WebsiteMenuItemCtrl Ftmp = null;

			// Compare ids
			switch (menuItemId)
			{
				case eMenuItem.SearchPopup:
					Ftmp = menuSearchPopup;
					break;

				case eMenuItem.ChaseScreen:
					Ftmp = menuChaseScreen;
					break;
				case eMenuItem.ChaseScreen_Contacts:
					Ftmp = menuChaseScreen_Contacts;
					break;
				case eMenuItem.ChaseScreen_DebtDetails:
					Ftmp = menuChaseScreen_DebtDetails;
					break;
				case eMenuItem.ChaseScreen_Communications:
                    Ftmp = menuChaseScreen_Communications;
					break;
                case eMenuItem.ChaseScreen_Tools:
					Ftmp = menuChaseScreen_Tools;
					break;
				case eMenuItem.ChaseScreen_Comments:
					Ftmp = menuChaseScreen_Comments;
					break;
				case eMenuItem.ChaseScreen_Agent:
					Ftmp = menuChaseScreen_Agent;
					break;

				case eMenuItem.UserManagement:
                    Ftmp = menuUserManagement;
					break;

				case eMenuItem.Dashboard:
					Ftmp = menuDashboard;
					break;

				case eMenuItem.Admin:
					Ftmp = menuAdmin;
					break;
                case eMenuItem.Admin_Agents:
                    Ftmp = menuAdmin_Agents;
                    break;
                case eMenuItem.Admin_Communications:
                    Ftmp = menuAdmin_Communications;
                    break;
                case eMenuItem.Admin_Financial:
                    Ftmp = menuAdmin_Financial;
                    break;
                case eMenuItem.Admin_ImportWizard:
					Ftmp = menuAdmin_ImportWizard;
					break;
                case eMenuItem.Admin_Invoicing:
                    Ftmp = menuAdmin_Invoicing;
					break;
				case eMenuItem.Admin_BulkActions:
					Ftmp = menuAdmin_BulkActions;
					break;
				case eMenuItem.Admin_Configurations:
					Ftmp = menuAdmin_Configurations;
					break;

				case eMenuItem.Workflow:
					Ftmp = menuWorkflows;
					break;

				case eMenuItem.ClientControls:
					Ftmp = menuClientControls;
					break;

				case eMenuItem.ClientControl_Settings:
                    Ftmp = menuClientControl_Settings;
					break;
				case eMenuItem.ClientControl_Rates:
                    Ftmp = menuClientControl_Rates;
					break;
				case eMenuItem.ClientControl_Reports:
                    Ftmp = menuClientControl_Reports;
					break;
				case eMenuItem.ClientControl_Cases:
                    Ftmp = menuClientControl_Cases;
					break;

                //case eMenuItem.InvoiceControls:
                //    Ftmp = menuInvoice;
                //    break;

				case eMenuItem.Reports:
					Ftmp = menuReports;
					break;

                case eMenuItem.SignOut:
					Ftmp = menuSignOut;
					break;

				default:
#if DEBUG
					Debug.Assert(false);
#endif // #if DEBUG
					break;

			}

			return Ftmp;

		}

		private void _setHighlight(eMenuItem menuItemId, bool bHighlight)
		{

			// Shortcuts
			if (menuItemId == eMenuItem.NotSet)
				return;

			// Find the menu item for this id
			WebsiteMenuItemCtrl menuItem = _getMenuItem(menuItemId);
#if DEBUG
			Debug.Assert(menuItem != null);
#endif // #if DEBUG
			menuItem.IsHighlighted = bHighlight;

			// If highlighting...
			if (bHighlight)
			{

				// Cannot click a highlighted menu item
				menuItem.IsLink = false;

				// Pin the parent
				switch (menuItemId)
				{
					case eMenuItem.ChaseScreen:
					case eMenuItem.ChaseScreen_Contacts:
					case eMenuItem.ChaseScreen_DebtDetails:
					case eMenuItem.ChaseScreen_Communications:
					case eMenuItem.ChaseScreen_Tools:
					case eMenuItem.ChaseScreen_Comments:
					case eMenuItem.ChaseScreen_Agent:
						//ChasePinButton.PinnedState = true;
						break;

					case eMenuItem.Admin:
					case eMenuItem.Admin_Agents:
					case eMenuItem.Admin_Communications:
					case eMenuItem.Admin_Financial:
					case eMenuItem.Admin_ImportWizard:
                    case eMenuItem.Admin_Invoicing:
					case eMenuItem.Admin_BulkActions:
					case eMenuItem.Admin_Configurations:
						//AdminPinButton.PinnedState = true;
						break;

					case eMenuItem.ClientControls:
                    case eMenuItem.ClientControl_Settings:
                    case eMenuItem.ClientControl_Rates:
                    case eMenuItem.Reports:
					case eMenuItem.ClientControl_Cases:
						//ReportsPinButton.PinnedState = true;
						break;

				} 

			}

		}

	}

}
